package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.util.SparseArray;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.packers.EngineeringCompanyEntity;
import com.sgb.kjwl.model.entity.packers.LeaguerCompanyEntity;
import com.sgb.kjwl.model.entity.packers.MemberCompanyEntity;
import com.sgb.kjwl.model.entity.self.CompanyFocusEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.net.entity.ListResponse;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author younghow
 * @date 2021/8/18
 * description:工队首页上半部分的viewModel
 */
public class PackerChangeFirstViewModel extends AndroidViewModel {

    public MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> successLiveData = new MutableLiveData<>();
    public MutableLiveData<Boolean> errorLiveData = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity>> mSuccessWorkType = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mSuccessWorkTypeForPop = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity>> mSuccessIntelligenceLevel = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity>> mSuccessMemberCompany = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mSuccessArea = new MutableLiveData<>();
    public MutableLiveData<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> mSuccessWorkKind = new MutableLiveData<>();
    public MutableLiveData<SparseArray<List<AddressEntity>>> mSuccessCompany = new MutableLiveData<>();
    SparseArray<List<AddressEntity>> companyList = new SparseArray<>();


    public PackerChangeFirstViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 这是用来获取工队首页的顶部标题的
     *
     * @return 工队首页的数据
     */
    public List<Integer> getContentTitle() {
        List<Integer> list = new ArrayList<>();
        list.add(R.mipmap.packers_team_need);
        list.add(R.mipmap.packers_team_person);
        list.add(R.mipmap.packers_team);
        list.add(R.mipmap.packers_team_price);
        list.add(R.mipmap.packers_building_subpackage);
        list.add(R.mipmap.packers_building_company);
        list.add(R.mipmap.packers_popular_team);
        return list;
    }

    public void getLinkAgeWorkType(boolean isInit) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getLinkAgeWorkType().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : response.body().getData()) {
                        AddressEntity entity = new AddressEntity("", datum.getName(), datum.getNo());
                        entity.setIconUrl(datum.getIconUrl());
                        list.add(entity);
                    }

                    if (isInit) {
                        mSuccessWorkType.postValue(list);
                    } else {
                        mSuccessWorkTypeForPop.postValue(response.body().getData());
                    }
                } else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getContent() {
        List<AddressEntity> list = new ArrayList<>();
        AddressEntity entity1 = new AddressEntity("", "施工队", "");
        entity1.setIconResourceID(R.mipmap.icon_shigongdui);
        list.add(entity1);
        AddressEntity entity2 = new AddressEntity("", "班组工队", "");
        entity2.setIconResourceID(R.mipmap.icon_banzugongdui);
        list.add(entity2);
        AddressEntity entity3 = new AddressEntity("", "工程公司", "");
        entity3.setIconResourceID(R.mipmap.icon_gongchenggongsi);
        list.add(entity3);
        AddressEntity entity4 = new AddressEntity("", "劳务公司", "");
        entity4.setIconResourceID(R.mipmap.icon_laowugongsi);
        list.add(entity4);
        mSuccessWorkType.postValue(list);
    }

    public void getCategoryListTree() {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getCategoryListTree().enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>> datum : response.body().getData()) {
                        AddressEntity entity = new AddressEntity("", datum.getName(), datum.getNo());
                        entity.setIconUrl(datum.getIconUrl());
                        list.add(entity);
                    }
                    mSuccessWorkType.postValue(list);
                } else {
                    errorLiveData.postValue(true);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    /**
     * 薪资方式
     *
     * @return 薪资方式的列表
     */
    public List<PickSelectEntity> getSalaryFs() {
        List<PickSelectEntity> listFs = new ArrayList<>();
        PickSelectEntity entity1 = new PickSelectEntity();
        entity1.setNo("3");
        entity1.setProjectscale("自定义薪资");
        PickSelectEntity entity2 = new PickSelectEntity<>();
        entity2.setNo("2");
        entity2.setProjectscale("电议");
        PickSelectEntity entity3 = new PickSelectEntity();
        entity3.setNo("1");
        entity3.setProjectscale("面议");
        listFs.add(entity1);
        listFs.add(entity2);
        listFs.add(entity3);
        return listFs;
    }

    public void getIntelligenceLevel() {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().intelligenceLevel().enqueue(new ICallBack<BaseEntity<List<AddressEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity>> data) {
                if (data.isSuccess()) {
                    List<AddressEntity> list = new ArrayList<>();
                    list.add(new AddressEntity("", "特级", ""));
                    list.add(new AddressEntity("", "一级", ""));
                    list.add(new AddressEntity("", "二级", ""));
                    list.add(new AddressEntity("", "三级", ""));
                    mSuccessIntelligenceLevel.postValue(list);
                }
            }
        });
    }

    public void leaguerCompany() {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().leaguerCompany(6, 1).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaguerCompanyEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<LeaguerCompanyEntity>>> data) {
                if (data.isSuccess()) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (LeaguerCompanyEntity leaguerCompanyEntity : data.getData().getList()) {
                        AddressEntity pickFilterEntity = new AddressEntity();
                        pickFilterEntity.setName(leaguerCompanyEntity.getCompanyName());
                        pickFilterEntity.setNo(leaguerCompanyEntity.getCompanyNo());
                        list.add(pickFilterEntity);
                    }
                    mSuccessMemberCompany.postValue(list);
                }
            }
        });
    }

    public void memberCompany() {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().memberCompany(2, 6, 1).enqueue(new ICallBack<BaseEntity<ListResponse<List<MemberCompanyEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MemberCompanyEntity>>> data) {
                if (data.isSuccess()) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (MemberCompanyEntity memberCompanyEntity : data.getData().getList()) {
                        AddressEntity pickFilterEntity = new AddressEntity();
                        pickFilterEntity.setName(memberCompanyEntity.getName());
                        pickFilterEntity.setNo(memberCompanyEntity.getBusinessNo());
                        list.add(pickFilterEntity);
                    }
                    mSuccessMemberCompany.postValue(list);
                }
            }
        });
    }

    public void getIntelligenceType() {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().intelligenceType().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity>>>> data) {
                if (data.isSuccess()) {
                    if (data.getState().equals(APIManager.RESPONSE_OK)) {
                        List<AddressEntity> list = new ArrayList<>();
                        if (data.getData().size() > 18) {
                            for (int i = 0; i < 19; i++) {
                                list.add(data.getData().get(i));
                            }
                        } else {
                            list.addAll(data.getData());
                        }
                        AddressEntity entity = new AddressEntity("", "其他类型", "");
                        entity.setIconResourceID(R.mipmap.icon_qitaleixing);
                        list.add(entity);
                        mSuccessWorkType.postValue(list);
                    }
                }
            }
        });
    }

    public void getCompanyList(int page, String userNo) {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getUserAPI().getCompanyFocus(page, 6, userNo).enqueue(new Callback<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>> call, Response<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (CompanyFocusEntity memberCompanyEntity : response.body().getData().getList()) {
                        AddressEntity pickFilterEntity = new AddressEntity();
                        pickFilterEntity.setName(memberCompanyEntity.getCompanyName());
                        pickFilterEntity.setNo(memberCompanyEntity.getCompanyNo());
                        list.add(pickFilterEntity);
                    }
                    mSuccessMemberCompany.postValue(list);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityList() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mSuccessArea.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                errorLiveData.postValue(true);
            }
        });
    }

    public void getNewList() {
        Map<String, Object> map = new HashMap<>();
        map.put("limit", 6);
        map.put("page", 1);
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEngineeringList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> call, Response<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        List<AddressEntity> list = new ArrayList<>();
                        for (EngineeringCompanyEntity memberCompanyEntity : response.body().getData().getList()) {
                            AddressEntity pickFilterEntity = new AddressEntity();
                            pickFilterEntity.setName(memberCompanyEntity.getName());
                            pickFilterEntity.setNo(memberCompanyEntity.getCompanyNo());
                            list.add(pickFilterEntity);
                        }
                        mSuccessMemberCompany.postValue(list);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> call, Throwable t) {
                successLiveData.postValue(true);
            }
        });
    }


    public void getCategoryType() {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getPackerAPI().getCategoryListTree(0).enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    mSuccessWorkKind.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>> call, Throwable t) {
                successLiveData.postValue(true);
            }
        });
    }

    public void getRecommendList(int page, String userNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("limit", 27);
        map.put("page", 1);
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().getEngineeringList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> call, Response<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> response) {
                successLiveData.postValue(true);
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null) {
                        List<AddressEntity> list = new ArrayList<>();
                        for (EngineeringCompanyEntity memberCompanyEntity : response.body().getData().getList()) {
                            AddressEntity pickFilterEntity = new AddressEntity();
                            pickFilterEntity.setName(memberCompanyEntity.getName());
                            pickFilterEntity.setNo(memberCompanyEntity.getCompanyNo());
                            list.add(pickFilterEntity);
                        }
                        companyList.append(0,list);
                        getLeaguerCompany(page,userNo);
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EngineeringCompanyEntity>>>> call, Throwable t) {
                successLiveData.postValue(true);
            }
        });
    }

    public void getMemberCompany(int page, String userNo) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().memberCompany(2, 27, 1).enqueue(new ICallBack<BaseEntity<ListResponse<List<MemberCompanyEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MemberCompanyEntity>>> data) {
                if (data.isSuccess()) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (MemberCompanyEntity memberCompanyEntity : data.getData().getList()) {
                        AddressEntity pickFilterEntity = new AddressEntity();
                        pickFilterEntity.setName(memberCompanyEntity.getName());
                        pickFilterEntity.setNo(memberCompanyEntity.getBusinessNo());
                        list.add(pickFilterEntity);
                    }
                    companyList.append(2,list);
                    getAttendCompanyList(page,userNo);
                }
            }
        });
    }

    public void getLeaguerCompany(int page, String userNo) {
        loadingLiveData.postValue(true);
        NewAPIManager.getInstance().getTeamNewInterfaceAPI().leaguerCompany(27, 1).enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaguerCompanyEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<LeaguerCompanyEntity>>> data) {
                if (data.isSuccess()) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (LeaguerCompanyEntity leaguerCompanyEntity : data.getData().getList()) {
                        AddressEntity pickFilterEntity = new AddressEntity();
                        pickFilterEntity.setName(leaguerCompanyEntity.getCompanyName());
                        pickFilterEntity.setNo(leaguerCompanyEntity.getCompanyNo());
                        list.add(pickFilterEntity);
                    }
                    companyList.append(1,list);
                    getMemberCompany(page,userNo);
                }
            }
        });
    }

    public void getAttendCompanyList(int page, String userNo) {
        loadingLiveData.postValue(true);
        APIManager.getInstance().getUserAPI().getCompanyFocus(page, 27, userNo).enqueue(new Callback<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>> call, Response<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    List<AddressEntity> list = new ArrayList<>();
                    for (CompanyFocusEntity memberCompanyEntity : response.body().getData().getList()) {
                        AddressEntity pickFilterEntity = new AddressEntity();
                        pickFilterEntity.setName(memberCompanyEntity.getCompanyName());
                        pickFilterEntity.setNo(memberCompanyEntity.getCompanyNo());
                        list.add(pickFilterEntity);
                    }
                    companyList.append(3,list);
                    mSuccessCompany.postValue(companyList);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<com.swgk.core.base.model.entity.ListResponse<List<CompanyFocusEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
